package com.huxin.servelt.admin;


import com.alibaba.fastjson.JSON;
import com.huxin.pojo.BookHisInfo;
import com.huxin.pojo.BooksInfo;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

/**
 * 管理员根据hid归还书籍
 */
@WebServlet("/admin/deleteBookHisInfoByHid")
public class AdminDeleteBookHisInfoByHidServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String hid = req.getParameter("hid");
        Userservice userservice = new Userservice();
        //根据hid查询该记录 借的书籍是否归还
        BookHisInfo bookHisInfo=userservice.findBookHisInfoById(hid);
//        if (bookHisInfo.getStatus()==2||bookHisInfo.getStatus().equals(2)||bookHisInfo.getStatus().equals("2")){// 归还才能执行删除
//            int i=userservice.deleteBookHisInfoByHid(hid);
//            resp.getWriter().write("1");//成功
//        }else {
//            resp.getWriter().write("2");//失败
//        }
        //只有未归还的书才有归还按钮
        bookHisInfo.setEnd_time(new Date(System.currentTimeMillis()));
        bookHisInfo.setStatus(2);
        int i = userservice.updateBookHisInfoByHid(bookHisInfo);
        if (i==1){
            //归还成功之后对应的数量要+1
            int i1 = userservice.addBookNum(bookHisInfo.getBid());
            //归还之后用户的最大借阅数量+1
            userservice.addUserMaxNum(bookHisInfo.getAid());
            if (i1==1){
                resp.getWriter().write("1");//成功
            }
        }else {
            resp.getWriter().write("2");//失败
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
